<?php

namespace plugin\admin\app\controller\common;

use plugin\admin\app\controller\Base;
use plugin\admin\app\model\AdminThink;
use think\facade\Db;
use yzh52521\Task\Client;

class Test extends Base
{
    public function crontab(){
        $param = [
            'method' => 'crontabCreate',
            'args'   => [
                'title'     => '输出 我的webman 版本',
                'type' =>1,
                'rule' => '*/30 * * * * *',
                'target'     => 'version',
                'status'    => 1,
                'remark'    => '每30秒执行',
            ]
        ];
        $request = Client::instance()->request($param);
        return json($request);
    }
    public function crontablist(){
        $param = [
            'method' => 'crontabIndex',//计划任务列表
            'args'   => ['limit' => 10, 'page' => 1]//参数
        ];
        $result= Client::instance()->request($param);
        return json($result);
    }
    public function logdelet()
    {
//        Db::table('wa_admin_crontab_log')->whereWeek('create_time', 'last week')->delete();
        $res = Db::table('wa_admin_crontab_log')->where('id', 1918)->delete();
        return !!$res;
    }
    public function admin()
    {
        $admin = AdminThink::hasWhere('roles', ['id' => 2])->select();
        return $this->json(0,'ok',$admin);
    }
    public function list(){
        $path = runtime_path() . '/logs/admin/info';
        $arrFiles = [];
        $iterator = new \FilesystemIterator($path);
        if(!$iterator){
            abort(1001,'请求失败，文件路径错误');
        }
        foreach($iterator as $entry) {
            $arr = [];
            $arr['name'] = $entry->getFilename();
            $arr['getPathname'] = $entry->getPathname();
            $arr['getSize'] = $entry->getSize();
            $arrFiles[] = $arr;
        }
        return $this->json(0,'ok',$arrFiles);
    }
    public function disk(){
        $dirPath = runtime_path() . '/logs/';
        $disk_size                = floor(disk_total_space($dirPath) / (1024 * 1024)) .'MB';

        $path = runtime_path() . '/logs/admin';
        $filesize = 0;
        $filearr = [];
        $iterator = new \FilesystemIterator($path);

        foreach($iterator as $entry) {
            $filearr[]['name'] = $entry->getFilename();
            $filearr[]['size'] = $entry->getSize();
            $filesize += $entry->getSize();
        }
        $resData = [
            '$disk_size'=>$disk_size,
            '$filearr'=>$filearr,
            '$filesize'=>$filesize,
        ];
        return $this->json(0,'日志容量',$resData);
    }
}